package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.service.AdminOrderService;
import com.cskaoyan.market.service.AdminOrderServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketOrderReadVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/order/*")
public class AdminOrderController extends HttpServlet {
    AdminOrderService adminOrderService = new AdminOrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op=requestURI.replace(req.getContextPath()+"/admin/order/","");
        if("list".equals(op)){
            list(req,resp);
        }
        if ("detail".equals(op)){
            detail(req,resp);
        }
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Map<String, Object> marketOrderReadVo =adminOrderService.detail(id);
        Object ok = ResponseUtil.ok(marketOrderReadVo);
        String s=JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page=req.getParameter("page");
        String limit=req.getParameter("limit");
        String sort=req.getParameter("sort");
        String order=req.getParameter("order");
        String orderStatusArray=req.getParameter("orderStatusArray");
        String status=req.getParameter("status");
        String end=req.getParameter("end");
        String userId=req.getParameter("userId");
        String OrderSn=req.getParameter("OrderSn");
        List<MarketOrder> orderList=adminOrderService.list(page,limit,sort,order,orderStatusArray,status,end,userId,OrderSn);
        Object ok= ResponseUtil.okList(orderList);
        String s= JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MarketOrder marketOrder=JacksonUtil.getObjectMapper().readValue(req.getReader(), MarketOrder.class);
        Integer id=adminOrderService.refund(marketOrder);

    }
}
